********************************************************************************
*
*    PROJECT: MENTORING, EDUCATIONAL PREFERENCES, AND CAREER CHOICE 
*
*    AUTHORS: HAYASHI, KIM, MATSUDA, PHAM
* 
*    DATE: MAY 2025
*
********************************************************************************

/* This dofile conducts analysis using the cleaned datasets

Inputs

* database on all grade 9-10 students at baseline
  "$clean/comparison_all_grade910_baseline.dta" 
  
* database on all grade 9-10 students in sampled-schools at baseline 
  "$clean/comparison_grade910_baseline_reachable_wgrade.dta"
  
* database on all grade 11-12 students at baseline
  "$clean/comparison_all_grade1112_baseline.dta" 
  
* database on all grade 11-12 students in sampled-schools at baseline 
  "$clean/comparison_grade1112_baseline_reachable_wgrade.dta"

* database on STEM mentors 
  "$clean/STEM_mentor"
	
* database on TVET mentors 
  "$clean/TVET_mentor"   
  
* database on discussion topics based on mentor report 
  "$clean/mentor_report_discussion.dta"
  
* database on STEM sample 
  "$clean/grade10_analysis.dta"   
  
* database on TVET sample 
  $clean/grade12_analysis.dta"  

*/

version 17
clear all
capture log close
set varabbrev off
set more off

cap log close 
local tcdate = subinstr("$S_DATE", " ", "", .)
global date "`tcdate'"
log using "$logfile/bhutan_mentoring_analysis_$date.log", replace 

cap program drop add_symbol 
program add_symbol
	scalar p = 2*ttail(e(df_r), abs(`=scalar(t)'))
	if `=scalar(p)' > 0.1 {
		scalar symbol = ""
	}
	if `=scalar(p)' < 0.1 & `=scalar(p)'>=0.05 {
		scalar symbol = "*"
	}
	if `=scalar(p)' < 0.05 & `=scalar(p)'>=0.01 {
		scalar symbol = "**"
	}
	if `=scalar(p)' < 0.01 {
		scalar symbol = "***"
	}
end


*****************************************************************************
*****************************************************************************
******************** IN-TEXT NUMBERS ****************************************
*****************************************************************************
*****************************************************************************

*** Mentor Characteristics 
****************************
use "$clean/STEM_mentor.dta", clear
gen grade = 10 
append using "$clean/TVET_mentor.dta"
replace grade = 12 if missing(grade)

lab def grade 10 "Grade 10" 12 "Grade 12", modify
lab val grade grade

* Previous experience with being a mentee
bys grade: tab mentor_beenmentee
bys grade: tab mentor_beenmentee_sat

* Experience with mentoring 
bys grade: tab mentor_exp
tab mentor_expstem

* Satisfaction with current study
bys grade: tab mentor_progrec

* Others 
lab var mentor_years "Year in college"
estpost tabstat mentor_age mentor_male mentor_years b5extro b5agree b5consci b5neuro b5open if grade==10, c(stat) stat(mean sd min max n) 
esttab using "$tabfig/mentor10.tex", replace ////
 cells("mean(fmt(%6.2fc)) sd(fmt(%6.2fc)) min max count") nonumber ///
  nomtitle nonote noobs label booktabs ///
  collabels("Mean" "SD" "Min" "Max" "N")

estpost tabstat mentor_age mentor_male b5extro b5agree b5consci b5neuro b5open if grade==12, c(stat) stat(mean sd min max n) 
esttab using "$tabfig/mentor12.tex", replace ////
 cells("mean(fmt(%6.2fc)) sd(fmt(%6.2fc)) min max count") nonumber ///
  nomtitle nonote noobs label booktabs ///
  collabels("Mean" "SD" "Min" "Max" "N")


*** Mentor-Mentee Meetings
****************************
* General information
use "$clean/grade10_analysis.dta", clear
gen grade = 10 
append using "$clean/grade12_analysis.dta"
replace grade = 12 if missing(grade)

* Summary of the mentor-mentee meetings that were online
su num_online if grade == 10
su num_online if grade == 12

* Summary of the communication beyond official meetings
su communication if grade == 10
su communication if grade == 12

su communication_m if grade == 10
su communication_m if grade == 12

* Distribution of the middle 80% length of each meeting
foreach grade in 10 12 { 
	forval i=1/4 {
	graph box meeting_minutes`i' if grade==`grade', noout name(m`i', replace) subtitle("meeting `i'")
	}
	gr combine m1 m2 m3 m4, c(4) ycommon
	gr export "$tabfig/meeting_length`grade'.png", as(png) replace
}

* Distribution of discussion topics 
use "$clean/mentor_report_discussion.dta", clear 
lab def grade 10 "Grade 10" 12 "Grade 12", modify
lab val grade grade

graph bar share, over(topic) over(thenumberofthemeeting, gap(0)) over(grade) stack asyvars ///
    legend(order(1 "Importance of STEM/TVET" 2 "Student Life Experience" 3 "Courses and Study Options" 4 "Career Path" 5 "Labor Market Information" 6 "Entry Requirement" 7 "Application Process" 8 "General Perception of TVET/STEM" 9 "Others") size(small)) ///
    ytitle("Share (%)") 
gr export "$tabfig/discussion_topics.png", as(png) replace

	
	
*****************************************************************************
*****************************************************************************
********* REPRESENTATIVENESS OF THE STUDY SAMPLE CHARACTERISTICS ************
*****************************************************************************
*****************************************************************************

use  "$clean/comparison_grade910_baseline_reachable_wgrade.dta", clear
gen sample10 = 1
append using "$clean/comparison_all_grade910_baseline.dta"
replace sample10 = 0 if missing(sample10)
gen grade = 10

append using "$clean/comparison_grade1112_baseline_reachable_wgrade.dta" 
gen sample12 = 1 if missing(sample10)
append using "$clean/comparison_all_grade1112_baseline.dta"
replace sample12 = 0 if missing(sample12) & missing(sample10)
replace grade = 12 if missing(grade)

gl grade10_comparison "age_bl male father_edu mother_edu perf_math_bl perf_scie_bl big5extro_bl big5agree_bl big5consci_bl big5neuro_bl big5open_bl i_sci_top1_bl i_e6_bl like_math_bl like_scie_bl attitude_stem_bl genderbias_bl subj_sci_bl e9c_bl approval_sci_bl e16c_bl ability_bl"

gl grade12_comparison "age_bl male father_edu mother_edu perf_eng_bl perf_lang_bl big5extro_bl big5agree_bl big5consci_bl big5neuro_bl big5open_bl i_f9g_bl i_f14g_bl bluecollarbias_bl attitude_stem_bl genderbias_tvet_bl  subj_tvet_bl satisfied_tvet_bl approval_tvet_bl f16g_bl ability_tvet_bl i_g4b_bl i_g5b_bl "

cap erase "$tabfig/comparison_sample10.tex"
cap erase "$tabfig/comparison_sample12.tex"

foreach grade in 10 12 {
	
	foreach i in comparison {
	foreach x of global grade`grade'_`i' {
		
		qui sum `x' if grade == `grade'				
		* obtain labels
		local Label`x': variable label `x'
	
		* obtain # observations
		qui sum `x' if sample`grade' == 0 & grade == `grade'
		local N: dis %3.0f r(N)
		local Ns = "`N'"
	
		* obtain mean of the whole sample 
		qui sum `x' if sample`grade' == 0 & grade==`grade'
		local meanC: dis %3.2f r(mean)
		local meanCs = "`meanC'"
		
		* obtain mean of the study sample 
		qui sum `x' if sample`grade' == 1 & grade==`grade'
		local meanT: dis %3.2f r(mean)
		local meanTs = "`meanT'"
	
		* obtain p-value of the difference between the study sample and the whole sample
		qui reg `x' sample`grade' if grade==`grade', r
		
		scalar t = _b[sample`grade']/_se[sample`grade']
		local p = 2*ttail(e(df_r), abs(`=scalar(t)'))
		local pvalue: dis %4.3f `p'
		local pvalues: dis "`pvalue'"
		
		if inlist("`x'", "age_bl") {
			mata R = ("", "", "", "" \ "`N'", "`meanCs'", "`meanTs'", "`pvalues'")
			mmat2tex R using "$tabfig/comparison_sample`grade'.tex", append rownames("\emph{Panel A: Demographics}" "`Label`x''")
		} 
		else if inlist("`x'", "i_sci_top1_bl", "i_f9g_bl") {
			mata R = ("", "", "", "" \ "", "", "", "" \ "`N'", "`meanCs'", "`meanTs'", "`pvalues'")
			mmat2tex R using "$tabfig/comparison_sample`grade'.tex", append rownames("" "\emph{Panel B: Primary Outcomes}" "`Label`x''")
		} 
		else if inlist("`x'", "i_e6_bl", "bluecollarbias_bl") {
			mata R = ("", "", "", "" \ "", "", "", "" \ "`N'", "`meanCs'", "`meanTs'", "`pvalues'")
			mmat2tex R using "$tabfig/comparison_sample`grade'.tex", append rownames("" "\emph{Panel C: Intermediate Outcomes}" "`Label`x''")
		} 
		else {
			mata R = ("`N'", "`meanCs'", "`meanTs'", "`pvalues'")
			mmat2tex R using "$tabfig/comparison_sample`grade'.tex", append rownames("`Label`x''")
		}
		}
	}
}
	
	

*****************************************************************************
*****************************************************************************
************************ BASELINE CHARACTERISTICS ***************************
*****************************************************************************
*****************************************************************************
use "$clean/grade10_analysis.dta", clear
gen grade = 10 
append using "$clean/grade12_analysis.dta"
replace grade = 12 if missing(grade)

gl grade10 "age_bl male father_edu mother_edu i_sci_top1_bl i_e6_bl approval_sci_bl attitude_stem_bl genderbias_bl mt_parents mt_friends mt_teachers" 
estpost tabstat $grade10 if grade == 10, c(stat) stat(mean sd n) 
esttab using "$tabfig/sample10.tex", replace ////
 cells("mean(fmt(%6.2fc)) sd(fmt(%6.2fc)) count(fmt(%6.0fc))") nonumber ///
  nomtitle nonote noobs label booktabs ///
  collabels("Mean" "SD" "N") f 

gl grade12 "age_bl male father_edu mother_edu i_1f9g_bl i_f9g_bl i_1f14g_bl i_f14g_bl approval_tvet_bl attitude_stem_bl genderbias_tvet_bl bluecollarbias_bl mt_parents mt_friends mt_teachers"
estpost tabstat $grade12 if grade == 12, c(stat) stat(mean sd n) 
esttab using "$tabfig/sample12.tex", replace ////
 cells("mean(fmt(%6.2fc)) sd(fmt(%6.2fc)) count(fmt(%6.0fc))") nonumber ///
  nomtitle nonote noobs label booktabs ///
  collabels("Mean" "SD" "N") f


* Figure on errors in beliefs about employment rates
gr box error_g1b_bl, over(male) ytitle("errors in beliefs about employment rate (pp)") name(m1, replace) yline(0, lc(red) lp(s))
gr box error_g2b_bl, over(male) ytitle("errors in beliefs about earnings (log points)") name(m2, replace) yline(0, lc(red) lp(s))
gr combine m1 m2, c(2) scale(1.3) subtitle("TVET Degree", size(small)) 
gr export "$tabfig/belief_tvet.png", as(png) replace 

gr box error_g1a_bl, over(male) ytitle("errors in beliefs about employment rate (pp)") name(m1, replace) yline(0, lc(red) lp(s))
gr box error_g2a_bl, over(male) ytitle("errors in beliefs about earnings (log points)") name(m2, replace) yline(0, lc(red) lp(s))
gr combine m1 m2, c(2) scale(1.3) subtitle("University Degrees", size(small))
gr export "$tabfig/belief_uni.png", as(png) replace 

gr box error_g1c_bl, over(male) ytitle("errors in beliefs about employment rate (pp)") name(m1, replace) yline(0, lc(red) lp(s))
gr box error_g2c_bl, over(male) ytitle("errors in beliefs about earnings (log points)") name(m2, replace) yline(0, lc(red) lp(s))
gr combine m1 m2, c(2) scale(1.3) subtitle("High School Diploma", size(small))
gr export "$tabfig/belief_HS.png", as(png) replace 



*****************************************************************************
*****************************************************************************
*************************** BALANCE TEST ************************************
*****************************************************************************
*****************************************************************************

use "$clean/grade10_analysis.dta", clear
gen grade = 10 
append using "$clean/grade12_analysis.dta"
replace grade = 12 if missing(grade)

gl grade10_primary "i_sci_top1_bl"
gl grade10_secondary "i_e6_bl z_like_math_bl z_like_scie_bl z_like_all_bl z_attitude_stem_bl z_genderbias_bl z_subj_sci_bl z_e9c_bl z_approval_sci_bl z_e16c_bl z_ability_bl"
gl grade10_covariates "age_bl male father_edu mother_edu z_wealth_bl z_perf_math_bl z_perf_scie_bl z_perf_oths_bl z_perf_all_bl potentialshare z_big5extro_bl z_big5agree_bl z_big5consci_bl z_big5neuro_bl z_big5open_bl"

gl grade12_primary "i_f9g_bl i_f14g_bl" 
gl grade12_secondary "z_bluecollarbias_bl z_attitude_stem_bl z_genderbias_tvet_bl z_subj_tvet_bl z_satisfied_tvet_bl z_approval_tvet_bl z_f16g_bl z_ability_tvet_bl i_g4b_bl i_g5b_bl"
gl grade12_covariates "age_bl male father_edu mother_edu z_wealth_bl z_perf_eng_bl z_perf_lang_bl z_perf_all_bl potentialshare z_big5extro_bl z_big5agree_bl z_big5consci_bl z_big5neuro_bl z_big5open_bl"


******* Balance checks of missing values of all variables at baseline
****************************************************************************
cap erase "$tabfig/balance_missing10.tex"
cap erase "$tabfig/balance_missing12.tex"

cap drop m10_* m12_*

foreach grade in 10 12 {
	foreach i in primary secondary covariates {
		foreach x of global grade`grade'_`i' {	
			cap gen m`grade'_`x' = missing(`x') 
			replace m`grade'_`x' = . if grade~=`grade'	
		}
	}
	
	foreach i in primary secondary covariates {
	foreach x of global grade`grade'_`i' {
		
		qui sum m`grade'_`x' if grade == `grade'
		if r(mean) ~= 0 {					
		* obtain labels
		local Label`x': variable label `x'
	
		* obtain # observations
		qui sum m`grade'_`x' if grade == `grade'
		local N: dis %3.0f r(N)
		local Ns = "`N'"
	
		* obtain mean Control 
		qui sum m`grade'_`x' if CONTROL == 1 & grade==`grade'
		local meanC: dis %3.2f r(mean)
		local meanCs = "`meanC'"
		
		* obtain difference between Treatment/Spillover and Control 
		qui areg m`grade'_`x' TREAT SPILL if grade==`grade', a(strata) cl(school)
		foreach group in TREAT SPILL {
			local diff`group': dis %4.2f _b[`group']
			local diff`group's: dis "`diff`group''"
			
			scalar t = _b[`group']/_se[`group']
			local p`group' = 2*ttail(e(df_r), abs(`=scalar(t)'))
			local pvalue`group': dis %4.2f `p`group''
			local pvalue`group's: dis "`pvalue`group''"
		}
		
		* obtain the difference between treatment and spillover 
		qui areg m`grade'_`x' TREAT if CONTROL!=1 & grade==`grade', a(school)
			local dTREAT: dis %4.2f _b[TREAT]
			local dTREATs: dis "`dTREAT'"
			scalar t = _b[TREAT]/_se[TREAT]
			local p = 2*ttail(e(df_r), abs(`=scalar(t)'))
			local pvalue: dis %4.2f `p'
			local pvalues: dis "`pvalue'"

		* export results 
		mata R = ("`meanCs'", "`diffTREATs'" , "`pvalueTREATs'", "`diffSPILLs'" , "`pvalueSPILLs'", "`dTREATs'", "`pvalues'" )
		mmat2tex R using "$tabfig/balance_missing`grade'.tex", append rownames("`Label`x''")
		}
		}
	}
}




************** Balance checks of values of all variables at baseline
****************************************************************************
cap erase "$tabfig/balance_grade10.tex"
cap erase "$tabfig/balance_grade12.tex"

cap drop m10_* m12_*

foreach grade in 10 12 {	
	foreach i in covariates primary secondary {
	foreach x of global grade`grade'_`i' {
		
		qui sum `x' if grade == `grade'
		if r(mean) ~= 0 {					
		* obtain labels
		local Label`x': variable label `x'
	
		* obtain # observations
		qui sum `x' if grade == `grade'
		local N: dis %3.0f r(N)
		local Ns = "`N'"
	
		* obtain mean Control 
		qui sum `x' if CONTROL == 1 & grade==`grade'
		local meanC: dis %3.2f r(mean)
		local meanCs = "`meanC'"
		
		* obtain difference between Treatment/Spillover and Control 
		qui areg `x' TREAT SPILL if grade==`grade', a(strata) cl(school)
		foreach group in TREAT SPILL {
			local diff`group': dis %4.2f _b[`group']
			local diff`group's: dis "`diff`group''"
			
			scalar t = _b[`group']/_se[`group']
			local p`group' = 2*ttail(e(df_r), abs(`=scalar(t)'))
			local pvalue`group': dis %4.2f `p`group''
			local pvalue`group's: dis "`pvalue`group''"
		}
		
		* obtain the difference between treatment and spillover 
		qui areg `x' TREAT if CONTROL!=1 & grade==`grade', a(school)
			local dTREAT: dis %4.2f _b[TREAT]
			local dTREATs: dis "`dTREAT'"
			scalar t = _b[TREAT]/_se[TREAT]
			local p = 2*ttail(e(df_r), abs(`=scalar(t)'))
			local pvalue: dis %4.2f `p'
			local pvalues: dis "`pvalue'"

		* export results
		if inlist("`x'", "age_bl") {
			mata R = ("", "", "", "", "", "", "", "" \ "`N'", "`meanCs'", "`diffTREATs'" , "`pvalueTREATs'", "`diffSPILLs'" , "`pvalueSPILLs'", "`dTREATs'", "`pvalues'")
			mmat2tex R using "$tabfig/balance_grade`grade'.tex", append rownames("\emph{Panel A: Demographics}" "`Label`x''")
		} 
		else if inlist("`x'", "i_sci_top1_bl", "i_f9g_bl") {
			mata R = ("", "", "", "", "", "", "", "" \ "", "", "", "", "", "", "", "" \ "`N'", "`meanCs'", "`diffTREATs'" , "`pvalueTREATs'", "`diffSPILLs'" , "`pvalueSPILLs'", "`dTREATs'", "`pvalues'")
			mmat2tex R using "$tabfig/balance_grade`grade'.tex", append rownames("" "\emph{Panel B: Primary Outcomes}" "`Label`x''")
		} 
		else if inlist("`x'", "i_e6_bl", "z_bluecollarbias_bl") {
			mata R = ("", "", "", "", "", "", "", "" \ "", "", "", "", "", "", "", "" \ "`N'", "`meanCs'", "`diffTREATs'" , "`pvalueTREATs'", "`diffSPILLs'" , "`pvalueSPILLs'", "`dTREATs'", "`pvalues'")
			mmat2tex R using "$tabfig/balance_grade`grade'.tex", append rownames("" "\emph{Panel C: Intermediate Outcomes}" "`Label`x''")
		} 
		else {
			mata R = ("`N'", "`meanCs'", "`diffTREATs'" , "`pvalueTREATs'", "`diffSPILLs'" , "`pvalueSPILLs'", "`dTREATs'", "`pvalues'")
			mmat2tex R using "$tabfig/balance_grade`grade'.tex", append rownames("`Label`x''")
		}
		}
		}
	}
}




*****************************************************************************
*****************************************************************************
********************** ATTRITION ANALYSIS ***********************************
*****************************************************************************
*****************************************************************************

use "$clean/grade10_analysis.dta", clear
gen grade = 10 
append using "$clean/grade12_analysis.dta"
replace grade = 12 if missing(grade)

est clear
// Probability of attrition by treatment and control individuals
foreach grade in 10 12 {
	local outcomelist "attrition1 attrition_test"
	if `grade' == 12 {
		local outcomelist "attrition1 attrition2 attrition_test"
	}
    
	foreach outcome of local outcomelist {
	qui sum `outcome' if CONTROL == 1 & grade == `grade', de
		qui local cmean_c: di %9.3f r(mean)
	
	eststo ta1`grade'n`outcome': areg `outcome' TREAT SPILL if grade == `grade', a(strata) cl(school)
	local r2a: di %5.3f e(r2)
	estadd local rsq "`r2a'"
	estadd local mean_outcome = "`cmean_c'"
		
	eststo dta1`grade'n`outcome': lincomest TREAT - SPILL
	}
}		
	local filename table_attrition_a 
	
	esttab ta110* ta112* using "$tabfig/`filename'.tex", f replace ///
	mgroups("STEM sample" "TVET sample", pattern(1 0 1 0 0) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) ///
	keep(TREAT) star(* 0.10 ** 0.05 *** 0.01) cell(b(fmt(3) star) se(fmt(3) par)) label noobs collabels(none) noline posthead(\midrule) ///
	mtitle("\shortstack{1st Follow-up \\ Survey}" "\shortstack{Test Score \\ Data }"  ///
	"\shortstack{1st Follow-up \\ Survey}" "\shortstack{2nd Follow-up \\ Survey}" "\shortstack{Test Score \\ Data }") ///
	varlabels(TREAT "Treatment")
	
	esttab ta110* ta112* using "$tabfig/`filename'.tex", f append ///
	keep(SPILL) star(* 0.10 ** 0.05 *** 0.01) cell(b(fmt(3) star) se(fmt(3) par)) noobs collabels(none) noline nonumber nomtitle ///
	scalar("N Observations" "rsq R-squared" "mean_outcome Control Mean")  varlabels(SPILL "Spillover")
	
	esttab dta110* dta112* using "$tabfig/`filename'.tex", f append ///
	varlabels((1) "p-value (TREAT)-(SPILL)=0") noobs nogap nomtitle nonumber cells(p(fmt(3))) collabels(none) noline postfoot(\midrule)
	

// Whether attrition individuals are different in terms of baseline values 
// of primary and secondary outcome variables 

cap erase "$tabfig/table_attrition_b_grade10.tex"
cap erase "$tabfig/table_attrition_b_grade12.tex"

foreach grade in 10 12 {	
	foreach i in primary secondary {
	foreach x of global grade`grade'_`i' {
		
		* obtain labels
		local Label`x': variable label `x'
	
		* obtain # observations
		qui sum `x' if grade == `grade'
		local N: dis %3.0f r(N)
		local Ns = "`N'"

		* obtain mean non-attriters 
		qui sum `x' if attrition1 == 0 & grade==`grade'
		local meanC: dis %3.2f r(mean)
		local meanCs = "`meanC'"
		
		* obtain difference between attriters and non-attriters
		qui areg `x' attrition1 if grade==`grade', a(strata) cl(school)
		foreach group in attrition1 {
			local diff`group': dis %4.2f _b[`group']
			local diff`group's: dis "`diff`group''"
			
			scalar t = _b[`group']/_se[`group']
			local p`group' = 2*ttail(e(df_r), abs(`=scalar(t)'))
			local pvalue`group': dis %4.2f `p`group''
			local pvalue`group's: dis "`pvalue`group''"
		}

		
		* export results
		if inlist("`x'", "i_sci_top1_bl", "i_f9g_bl") {
			mata R = ("", "", "", "" \ "`N'", "`meanCs'", "`diffattrition1s'" , "`pvalueattrition1s'")
			mmat2tex R using "$tabfig/table_attrition_b_grade`grade'.tex", append rownames("\emph{Panel A: Primary Outcomes}" "`Label`x''")
		} 
		else if inlist("`x'", "i_e6_bl", "z_bluecollarbias_bl") {
			mata R = ("", "", "", "" \ "`N'", "`meanCs'", "`diffattrition1s'" , "`pvalueattrition1s'")
			mmat2tex R using "$tabfig/table_attrition_b_grade`grade'.tex", append rownames("\emph{Panel B: Intermediate Outcomes}" "`Label`x''")
		} 
		else {
			mata R = ("`N'", "`meanCs'", "`diffattrition1s'" , "`pvalueattrition1s'")
			mmat2tex R using "$tabfig/table_attrition_b_grade`grade'.tex", append rownames("`Label`x''")
		}
		}
	}
}

// Whether baseline characteristics of attrition individuals in the treatment 
// and spillover are different from those in the control group 

cap erase "$tabfig/table_attrition_c_grade10.tex"
cap erase "$tabfig/table_attrition_c_grade12.tex"

foreach grade in 10 12 {	
	foreach i in primary secondary {
	foreach x of global grade`grade'_`i' {
		
		* obtain labels
		local Label`x': variable label `x'
	
		* obtain # observations
		qui sum `x' if attrition1 == 1 & grade == `grade' 
		local N: dis %3.0f r(N)
		local Ns = "`N'"
		
		if `N' ~= 0 {

		* obtain mean attriters of the control group
		qui sum `x' if attrition1 == 1 & CONTROL==1 & grade==`grade'
		local meanC: dis %3.2f r(mean)
		local meanCs = "`meanC'"
		
		* obtain difference between TREAT and SPILL among attriters
		qui areg `x' TREAT SPILL if attrition1 == 1 & grade==`grade', a(strata) cl(school)
		foreach group in TREAT SPILL {
			local diff`group': dis %4.2f _b[`group']
			local diff`group's: dis "`diff`group''"
			
			scalar t = _b[`group']/_se[`group']
			local p`group' = 2*ttail(e(df_r), abs(`=scalar(t)'))
			local pvalue`group': dis %4.2f `p`group''
			local pvalue`group's: dis "`pvalue`group''"
		}
		
		* export results
		if inlist("`x'", "i_sci_top1_bl", "i_f9g_bl") {
			mata R = ("", "", "", "" , "" , "" \ "`N'", "`meanCs'", "`diffTREATs'" , "`pvalueTREATs'", "`diffSPILLs'" , "`pvalueSPILLs'")
			mmat2tex R using "$tabfig/table_attrition_c_grade`grade'.tex", append rownames("\emph{Panel A: Primary Outcomes}" "`Label`x''")
		} 
		else if inlist("`x'", "i_e6_bl", "z_bluecollarbias_bl") {
			mata R = ("", "", "", "" , "" , "" \ "`N'", "`meanCs'", "`diffTREATs'" , "`pvalueTREATs'", "`diffSPILLs'" , "`pvalueSPILLs'")
			mmat2tex R using "$tabfig/table_attrition_c_grade`grade'.tex", append rownames("\emph{Panel B: Intermediate Outcomes}" "`Label`x''")
		} 
		else {
			mata R = ("`N'", "`meanCs'", "`diffTREATs'" , "`pvalueTREATs'", "`diffSPILLs'" , "`pvalueSPILLs'")
			mmat2tex R using "$tabfig/table_attrition_c_grade`grade'.tex", append rownames("`Label`x''")
		}
		}
	}
	}
}



*****************************************************************************
*****************************************************************************
************************** MAIN RESULTS *************************************
*****************************************************************************
*****************************************************************************

use "$clean/grade10_analysis.dta", clear
gen grade = 10 
append using "$clean/grade12_analysis.dta"
replace grade = 12 if missing(grade)

gl grade10_covariates "age_bl male father_edu mother_edu z_wealth_bl z_perf_math_bl z_perf_scie_bl z_perf_oths_bl z_perf_all_bl potentialshare z_big5extro_bl z_big5agree_bl z_big5consci_bl z_big5neuro_bl z_big5open_bl"
gl grade12_covariates "age_bl male father_edu mother_edu z_wealth_bl z_perf_eng_bl z_perf_lang_bl z_perf_all_bl potentialshare z_big5extro_bl z_big5agree_bl z_big5consci_bl z_big5neuro_bl z_big5open_bl"

* Create missing variables for covariates 
foreach grade in 10 12 {
	foreach x of global grade`grade'_covariates {
		cap gen miss_`x' = 0
		cap gen imputed_`x' = .
		cap replace miss_`x' = 1 if missing(`x') & grade==`grade'
		replace imputed_`x' = `x' if grade==`grade'
		su `x' if grade == `grade', de
		replace imputed_`x' = r(p50) if grade==`grade' & missing(`x')
	} 
}

* Outcome variables 
gl grade10_primary_e "i_sci_top1 i_sci_enrolled"
gl grade10_secondary_e "z_score_math z_score_phy z_score_che z_score_bio" 
gl grade10_attitude_e "i_e6 z_attitude_stem z_genderbias z_subj_sci z_obj_sci z_e9c z_approval_sci z_e16c z_ability z_like_math z_like_scie z_like_all passing_bcse"

gl grade12_primary_e "i_f9g i_f14g s_app_tvet s_adm_tvet" 
gl grade12_secondary_e  "a_app_tvet a_adm_tvet z_prep_lang z_prep_eng z_prep_all passing_bhsec z_score_eng z_score_dzo"
gl grade12_attitude_e "z_attitude_stem z_genderbias_tvet z_bluecollarbias z_subj_tvet z_obj_tvet z_satisfied_tvet z_approval_tvet z_f16g z_ability_tvet i_g4b i_g5b"

lab var i_sci_enrolled "Enrolled in science stream in grade 11 (dummy)"
lab var passing_bcse "Passed BCSE certificate (dummy)"
lab var passing_bhsec "Passed BHSEC certificate (dummy)"

* Create a "fake" baseline outcomes for all outcomes, value 0
foreach grade in 10 12 {
	foreach i in primary secondary attitude {
		foreach x of global grade`grade'_`i'_e {
			cap gen `x'_bl = 0
		} 
	}
}

* Covariates with missing dummies 
gl grade10_controls "imputed_age_bl imputed_male imputed_father_edu imputed_mother_edu imputed_z_wealth_bl imputed_z_perf_math_bl imputed_z_perf_scie_bl imputed_z_perf_oths_bl imputed_z_perf_all_bl imputed_potentialshare imputed_z_big5extro_bl imputed_z_big5agree_bl imputed_z_big5consci_bl imputed_z_big5neuro_bl imputed_z_big5open_bl"
gl grade10_Mdummies "miss_age_bl miss_male miss_father_edu miss_mother_edu miss_z_wealth_bl miss_z_perf_math_bl miss_z_perf_scie_bl miss_z_perf_oths_bl miss_z_perf_all_bl miss_potentialshare miss_z_big5extro_bl miss_z_big5agree_bl miss_z_big5consci_bl miss_z_big5neuro_bl miss_z_big5open_bl"

gl grade12_controls "imputed_age_bl imputed_male imputed_father_edu imputed_mother_edu imputed_z_wealth_bl imputed_z_perf_eng_bl imputed_z_perf_lang_bl imputed_z_perf_all_bl imputed_potentialshare imputed_z_big5extro_bl imputed_z_big5agree_bl imputed_z_big5consci_bl imputed_z_big5neuro_bl imputed_z_big5open_bl"
gl grade12_Mdummies "miss_age_bl miss_male miss_father_edu miss_mother_edu miss_z_wealth_bl miss_z_perf_eng_bl miss_z_perf_lang_bl miss_z_perf_all_bl miss_potentialshare miss_z_big5extro_bl miss_z_big5agree_bl miss_z_big5consci_bl miss_z_big5neuro_bl miss_z_big5open_bl"

cap erase "$tabfig/main_grade10_primary.tex"
cap erase "$tabfig/main_grade10_secondary.tex"
cap erase "$tabfig/main_grade10_attitude.tex"
cap erase "$tabfig/main_grade12_primary.tex"
cap erase "$tabfig/main_grade12_secondary.tex"
cap erase "$tabfig/main_grade12_attitude.tex"

foreach grade in 10 12 {
	qui rwolf ${grade`grade'_primary_e} ${grade`grade'_secondary_e} ${grade`grade'_attitude_e} if grade == `grade', indepvar(TREAT SPILL) method(areg) abs(strata) bl(_bl) controls(${grade`grade'_controls} ${grade`grade'_Mdummies}) reps(250) seed(10101) cl(school)

	foreach i in primary secondary attitude {
		foreach x of global grade`grade'_`i'_e {	
			* Romano-Wolf
			local rw_`x'_TREAT: dis %4.3f e(rw_`x'_TREAT)
			local rw_`x'_TREATs: dis "[`rw_`x'_TREAT']"
			local rw_`x'_SPILL: dis %4.3f e(rw_`x'_SPILL)
			local rw_`x'_SPILLs: dis "[`rw_`x'_SPILL']"
		}
	}
	
	qui rwolf ${grade`grade'_primary_e} ${grade`grade'_secondary_e} ${grade`grade'_attitude_e} if CONTROL!=1 & grade == `grade', indepvar(TREAT) method(areg) bl(_bl) controls(${grade`grade'_controls} ${grade`grade'_Mdummies}) abs(school) reps(250) seed(10101)

	foreach i in primary secondary attitude {
		foreach x of global grade`grade'_`i'_e {	
			* Romano-Wolf
			local rw_`x'_TREATC: dis %4.3f e(rw_`x')
			local rw_`x'_TREATCs: dis "[`rw_`x'_TREATC']"
		}
	}


	foreach i in primary secondary attitude {
	foreach x of global grade`grade'_`i'_e {		
	* obtain labels
	local Label`x': variable label `x'
	
	* obtain # observations
	qui sum `x' if grade == `grade'
	local N: dis %3.0f r(N)
	local Ns = "`N'"
	
	* obtain mean Control 
	qui sum `x' if CONTROL == 1 & grade==`grade'
	local meanC: dis %3.2f r(mean)
	local meanCs = "`meanC'"
	
	local sdC: dis %3.2f r(sd)
	local sdCs: dis "(`sdC')"
	
	* obtain difference coef & se between TREAT & CONTROL
	
	qui areg `x' TREAT SPILL `x'_bl ${grade`grade'_controls} ${grade`grade'_Mdummies} if grade==`grade', a(strata) cl(school)
	foreach group in TREAT SPILL {
	scalar t = _b[`group']/_se[`group']
	add_symbol
	
	local diff`group': dis %4.3f _b[`group']
	local diff`group's: dis "`diff`group''`=scalar(symbol)'"
		
	local diffse`group': dis %4.3f _se[`group']
	local diffse`group's: dis "(`diffse`group'')"	
	
		if `grade' == 12 {
			qui boottest `group', cl(school) qui nogr seed(10101)
			local pbt`group': dis %4.3f r(p)
			local pbt`group's: dis "$\left\lbrace `pbt`group'' \right\rbrace$"
		}
	}
	
	* obtain difference between TREAT & SPILL 
	qui areg `x' TREAT `x'_bl ${grade`grade'_controls} ${grade`grade'_Mdummies} if CONTROL!=1 & grade==`grade', a(school)
	scalar t = _b[TREAT]/_se[TREAT]
	add_symbol
	local diffTS: dis %4.3f _b[TREAT]
	local diffTSs: dis "`diffTS'`=scalar(symbol)'"
	
	local diffseTS: dis %4.3f _se[TREAT]
	local diffseTSs: dis "(`diffseTS')"
	
	* export results 
	if `grade' == 12 {
		mata R = ("`N'", "`meanC'", "`diffTREATs'" , "`diffSPILLs'", "`diffTSs'"  \ "", "`sdCs'", "`diffseTREATs'", "`diffseSPILLs'",  "`diffseTSs'" \ "", "", "`pbtTREATs'", "`pbtSPILLs'", "" \ "", "", "`rw_`x'_TREATs'", "`rw_`x'_SPILLs'", "`rw_`x'_TREATCs'" )
		if "`i'" == "primary" {
			mmat2tex R using "$tabfig/main_grade`grade'_primary.tex", append rownames("`Label`x''" "" "" "")
		}
		if "`i'" == "secondary" {
			mmat2tex R using "$tabfig/main_grade`grade'_secondary.tex", append rownames("`Label`x''" "" "" "")
		}
		if "`i'" == "attitude" {
			mmat2tex R using "$tabfig/main_grade`grade'_attitude.tex", append rownames("`Label`x''" "" "" "")
		}
	}
	
	
	if `grade' == 10 {
		mata R = ("`N'", "`meanC'", "`diffTREATs'" , "`diffSPILLs'", "`diffTSs'"  \ "", "`sdCs'", "`diffseTREATs'", "`diffseSPILLs'",  "`diffseTSs'" \ "", "", "`rw_`x'_TREATs'", "`rw_`x'_SPILLs'", "`rw_`x'_TREATCs'" )
		if "`i'" == "primary" {
			mmat2tex R using "$tabfig/main_grade`grade'_primary.tex", append rownames("`Label`x''" "" "")
		}
		if "`i'" == "secondary" {
			mmat2tex R using "$tabfig/main_grade`grade'_secondary.tex", append rownames("`Label`x''" "" "" )
		}
		if "`i'" == "attitude" {
			mmat2tex R using "$tabfig/main_grade`grade'_attitude.tex", append rownames("`Label`x''" "" "")
		}
	}
	}
	}
}


******* Results focusing on science, arts, commerce, rigzhung, and tvet preferences 
cap erase "$tabfig/main_grade10_streams.tex"

global streams i_sci_top1 i_art_top1 i_com_top1 i_rig_top1 i_TVET_top1

qui rwolf $streams if grade==10, indepvar(TREAT SPILL) method(areg) abs(strata) bl(_bl) controls(${grade10_controls} ${grade10_Mdummies}) reps(250) seed(10101) cl(school) 

foreach x of global streams {
	* Romano-Wolf
	local rw_`x'_TREAT: dis %4.3f e(rw_`x'_TREAT)
	local rw_`x'_TREATs: dis "[`rw_`x'_TREAT']"
	local rw_`x'_SPILL: dis %4.3f e(rw_`x'_SPILL)
	local rw_`x'_SPILLs: dis "[`rw_`x'_SPILL']"
}

qui rwolf $streams if CONTROL!=1 & grade==10, indepvar(TREAT) method(areg) abs(strata) bl(_bl) controls(${grade10_controls} ${grade10_Mdummies}) reps(250) seed(10101) cl(school) 
foreach x of global streams {	
	* Romano-Wolf
	local rw_`x'_TREATC: dis %4.3f e(rw_`x')
	local rw_`x'_TREATCs: dis "[`rw_`x'_TREATC']"
}

foreach x of global streams {	
	* obtain labels
	local Label`x': variable label `x'
	
	* obtain # observations
	qui sum `x' if grade == 10
	local N: dis %3.0f r(N)
	local Ns = "`N'"
	
	* obtain mean Control 
	qui sum `x' if CONTROL == 1 & grade== 10
	local meanC: dis %3.2f r(mean)
	local meanCs = "`meanC'"
	
	local sdC: dis %3.2f r(sd)
	local sdCs: dis "(`sdC')"
	
	* obtain difference coef & se between TREAT & CONTROL
	qui areg `x' TREAT SPILL `x'_bl ${grade10_controls} ${grade10_Mdummies} if grade==10, a(strata) cl(school)
	foreach group in TREAT SPILL {
	scalar t = _b[`group']/_se[`group']
	add_symbol
	
	local diff`group': dis %4.3f _b[`group']
	local diff`group's: dis "`diff`group''`=scalar(symbol)'"
		
	local diffse`group': dis %4.3f _se[`group']
	local diffse`group's: dis "(`diffse`group'')"	
	}
	
	* obtain difference between TREAT & SPILL 
	qui areg `x' TREAT `x'_bl ${grade10_controls} ${grade10_Mdummies} if CONTROL!=1 & grade==10, a(school)
	scalar t = _b[TREAT]/_se[TREAT]
	add_symbol
	local diffTS: dis %4.3f _b[TREAT]
	local diffTSs: dis "`diffTS'`=scalar(symbol)'"
	
	local diffseTS: dis %4.3f _se[TREAT]
	local diffseTSs: dis "(`diffseTS')"
	
	* export 
	mata R = ("`N'", "`meanC'", "`diffTREATs'" , "`diffSPILLs'", "`diffTSs'"  \ "", "`sdCs'", "`diffseTREATs'", "`diffseSPILLs'",  "`diffseTSs'" \ "", "", "`rw_`x'_TREATs'", "`rw_`x'_SPILLs'", "`rw_`x'_TREATCs'" )
	mmat2tex R using "$tabfig/main_grade10_streams.tex", append rownames("`Label`x''" "" "")
}


******* Test joint significance using seemingly unrelated regressions 

est clear 
foreach x of global grade10_attitude_e {
	reg `x' `x'_bl TREAT SPILL i.strata $grade10_controls $grade10_Mdummies if grade == 10
	eststo e_`x'
}
suest e_*, cluster(school)

test [e_z_attitude_stem_mean]TREAT = [e_z_genderbias_mean]TREAT ///
   = [e_z_subj_sci_mean]TREAT = [e_z_e9c_mean]TREAT ///
   = [e_z_e9c_mean]TREAT = [e_z_approval_sci_mean]TREAT ///
   = [e_z_e16c_mean]TREAT = [e_z_ability_mean]TREAT ///
   = [e_z_obj_sci_mean]TREAT = [e_i_e6_mean]TREAT = 0
   
   
est clear 
foreach x of global grade12_attitude_e {
	reg `x' `x'_bl TREAT SPILL i.strata $grade12_controls $grade12_Mdummies if grade == 12
	eststo e_`x'
}
suest e_*, cluster(school)

test [e_z_attitude_stem_mean]TREAT = [e_z_genderbias_tvet_mean]TREAT ///
   = [e_z_bluecollarbias_mean]TREAT = [e_z_subj_tvet_mean]TREAT ///
   = [e_z_obj_tvet_mean]TREAT = [e_z_satisfied_tvet_mean]TREAT ///
   = [e_z_approval_tvet_mean]TREAT = [e_z_f16g_mean]TREAT ///
   = [e_z_ability_tvet_mean]TREAT = [e_i_g4b_mean]TREAT = [e_i_g5b_mean]TREAT = 0  
   
   
******* Specific components of objective knowledge 
cap erase "$tabfig/main_grade12_obj.tex"

global obj_knowledge z_obj_tvet i_obj_tvet_crt i_obj_tvet_regis i_obj_tvet_int

qui rwolf $obj_knowledge if grade==12, indepvar(TREAT SPILL) method(areg) abs(strata) controls(${grade12_controls} ${grade12_Mdummies}) reps(250) seed(10101) cl(school) 

foreach x of global obj_knowledge {
	* Romano-Wolf
	local rw_`x'_TREAT: dis %4.3f e(rw_`x'_TREAT)
	local rw_`x'_TREATs: dis "[`rw_`x'_TREAT']"
	local rw_`x'_SPILL: dis %4.3f e(rw_`x'_SPILL)
	local rw_`x'_SPILLs: dis "[`rw_`x'_SPILL']"
}

qui rwolf $obj_knowledge if CONTROL!=1 & grade==12, indepvar(TREAT) method(areg) abs(strata) controls(${grade12_controls} ${grade12_Mdummies}) reps(250) seed(10101) cl(school) 
foreach x of global streams {	
	* Romano-Wolf
	local rw_`x'_TREATC: dis %4.3f e(rw_`x')
	local rw_`x'_TREATCs: dis "[`rw_`x'_TREATC']"
}

foreach x of global obj_knowledge {	
	* obtain labels
	local Label`x': variable label `x'
	
	* obtain # observations
	qui sum `x' if grade == 12
	local N: dis %3.0f r(N)
	local Ns = "`N'"
	
	* obtain mean Control 
	qui sum `x' if CONTROL == 1 & grade== 12
	local meanC: dis %3.2f r(mean)
	local meanCs = "`meanC'"
	
	local sdC: dis %3.2f r(sd)
	local sdCs: dis "(`sdC')"
	
	* obtain difference coef & se between TREAT & CONTROL
	qui areg `x' TREAT SPILL ${grade12_controls} ${grade12_Mdummies} if grade==12, a(strata) cl(school)
	foreach group in TREAT SPILL {
	scalar t = _b[`group']/_se[`group']
	add_symbol
	
	local diff`group': dis %4.3f _b[`group']
	local diff`group's: dis "`diff`group''`=scalar(symbol)'"
		
	local diffse`group': dis %4.3f _se[`group']
	local diffse`group's: dis "(`diffse`group'')"	
	
	qui boottest `group', cl(school) qui nogr seed(10101)
			local pbt`group': dis %4.3f r(p)
			local pbt`group's: dis "$\left\lbrace `pbt`group'' \right\rbrace$"
	}
	
	* obtain difference between TREAT & SPILL 
	qui areg `x' TREAT ${grade12_controls} ${grade12_Mdummies} if CONTROL!=1 & grade==12, a(school)
	scalar t = _b[TREAT]/_se[TREAT]
	add_symbol
	local diffTS: dis %4.3f _b[TREAT]
	local diffTSs: dis "`diffTS'`=scalar(symbol)'"
	
	local diffseTS: dis %4.3f _se[TREAT]
	local diffseTSs: dis "(`diffseTS')"
	
	* export 
	mata R = ("`N'", "`meanC'", "`diffTREATs'" , "`diffSPILLs'", "`diffTSs'"  \ "", "`sdCs'", "`diffseTREATs'", "`diffseSPILLs'",  "`diffseTSs'" \ "", "", "`pbtTREATs'", "`pbtSPILLs'", "" \ "", "", "`rw_`x'_TREATs'", "`rw_`x'_SPILLs'", "`rw_`x'_TREATCs'" )
	mmat2tex R using "$tabfig/main_grade12_obj.tex", append rownames("`Label`x''" "" "" "")
}
             

****************** LOCAL AVERAGE TREATMENT EFFECTS ****************************

cap program drop add_symbol2 
program add_symbol2
	scalar p = 2*ttail(e(Fdf2), abs(`=scalar(t)'))
	if `=scalar(p)' > 0.1 {
		scalar symbol = ""
	}
	if `=scalar(p)' < 0.1 & `=scalar(p)'>=0.05 {
		scalar symbol = "*"
	}
	if `=scalar(p)' < 0.05 & `=scalar(p)'>=0.01 {
		scalar symbol = "**"
	}
	if `=scalar(p)' < 0.01 {
		scalar symbol = "***"
	}
end

cap erase "$tabfig/main_grade10_late.tex"
cap erase "$tabfig/main_grade12_late.tex"

cap rename actualTREAT4 TREAT4

foreach grade in 10 12 {
	*** ITT 
	qui rwolf ${grade`grade'_primary_e} ${grade`grade'_secondary_e} ${grade`grade'_attitude_e} if grade == `grade', indepvar(TREAT SPILL) method(areg) abs(strata) bl(_bl) controls(${grade`grade'_controls} ${grade`grade'_Mdummies}) reps(250) seed(10101) cl(school)

	foreach i in primary {
		foreach x of global grade`grade'_`i'_e {	
			* Romano-Wolf
			local rw_`x'_TREAT: dis %4.3f e(rw_`x'_TREAT)
			local rw_`x'_TREATs: dis "[`rw_`x'_TREAT']"
		}
	}
	
	
	*** LATE
	qui rwolf ${grade`grade'_primary_e} ${grade`grade'_secondary_e} ${grade`grade'_attitude_e} if grade == `grade', indepvar(TREAT4) iv(TREAT) method(ivregress) bl(_bl) controls(SPILL ${grade`grade'_controls} ${grade`grade'_Mdummies} i.strata) reps(250) seed(10101) cl(school)

	foreach i in primary {
		foreach x of global grade`grade'_`i'_e {	
			* Romano-Wolf
			local rw_`x'_LATE: dis %4.3f e(rw_`x')
			local rw_`x'_LATEs: dis "[`rw_`x'_LATE']"
		}
	}
	
	foreach i in primary {
	foreach x of global grade`grade'_`i'_e {		
	* obtain labels
	local Label`x': variable label `x'
	
	* obtain # observations
	qui sum `x' if grade == `grade'
	local N: dis %3.0f r(N)
	local Ns = "`N'"
	
	* obtain mean Control 
	qui sum `x' if CONTROL == 1 & grade==`grade'
	local meanC: dis %3.2f r(mean)
	local meanCs = "`meanC'"
	
	local sdC: dis %3.2f r(sd)
	local sdCs: dis "(`sdC')"
	
	* obtain difference coef & se between TREAT & CONTROL: ITT
	qui areg `x' TREAT SPILL `x'_bl ${grade`grade'_controls} ${grade`grade'_Mdummies} if grade==`grade', a(strata) cl(school)
	foreach group in TREAT {
	scalar t = _b[`group']/_se[`group']
	add_symbol
	
	local diff`group': dis %4.3f _b[`group']
	local diff`group's: dis "`diff`group''`=scalar(symbol)'"
		
	local diffse`group': dis %4.3f _se[`group']
	local diffse`group's: dis "(`diffse`group'')"	
	
		if `grade' == 12 {
			qui boottest `group', cl(school) qui nogr seed(10101)
			local pbt`group': dis %4.3f r(p)
			local pbt`group's: dis "$\left\lbrace `pbt`group'' \right\rbrace$"
		}
	}
	
	* obtain TREAT effect: LATE 

	ivreg2 `x' (TREAT4 = TREAT) SPILL `x'_bl ${grade`grade'_controls} ${grade`grade'_Mdummies} i.strata if grade==`grade', cl(school)

		scalar t = _b[TREAT4]/_se[TREAT4]
		add_symbol2
	
		local diffLATE: dis %4.3f _b[TREAT4]
		local diffLATEs: dis "`diffLATE'`=scalar(symbol)'"
		dis "`diffLATEs'"
		
		local diffseLATE: dis %4.3f _se[TREAT4]
		local diffseLATEs: dis "(`diffseLATE')"	
	
		if `grade' == 12 {
			qui boottest TREAT4, cl(school) qui nogr seed(10101)
			local pbtLATE: dis %4.3f r(p)
			local pbtLATEs: dis "$\left\lbrace `pbtLATE' \right\rbrace$"
		}
	
	
	* export results 
	if `grade' == 12 {
		mata R = ("`N'", "`meanC'", "`diffTREATs'" , "`diffLATEs'"  \ "", "`sdCs'", "`diffseTREATs'", "`diffseLATEs'" \ "", "", "`pbtTREATs'", "`pbtLATEs'" \ "", "", "`rw_`x'_TREATs'", "`rw_`x'_LATEs'")
		if "`i'" == "primary" {
			mmat2tex R using "$tabfig/main_grade`grade'_late.tex", append rownames("`Label`x''" "" "" "")
		}
	}
	
	
	if `grade' == 10 {
		mata R = ("`N'", "`meanC'", "`diffTREATs'" , "`diffLATEs'"  \ "", "`sdCs'", "`diffseTREATs'", "`diffseLATEs'" \ "", "", "`rw_`x'_TREATs'", "`rw_`x'_LATEs'")
		if "`i'" == "primary" {
			mmat2tex R using "$tabfig/main_grade`grade'_late.tex", append rownames("`Label`x''" "" "")
		}
	}
	}
	}
}


*****************************************************************************
*****************************************************************************
************************** HETEROGENEITY ANALYSIS ***************************
*****************************************************************************
*****************************************************************************

* Only focus on primary outcomes 
* No adjustment for multiple hypothesis testing

******* HETEROGENEITY ANALYSIS BY GENDER: BASELINE BALANCE

foreach grade in 10 12 {
	cap erase "$tabfig/balance_grade`grade'_hetgender.tex"	
	
	foreach i in primary secondary {
	foreach x of global grade`grade'_`i' {		
	* obtain labels
	local Label`x': variable label `x'
	
	* obtain # observations
	qui sum `x' if grade == `grade'
	local N: dis %3.0f r(N)
	local Ns = "`N'"
	
	* obtain mean Control 
	qui sum `x' if CONTROL == 1 & grade==`grade'
	local meanC: dis %3.2f r(mean)
	local meanCs = "`meanC'"
	
	local sdC: dis %3.2f r(sd)
	local sdCs: dis "(`sdC')"
	
	* obtain coef & se of all combinations
	qui areg `x' TREAT_MM TREAT_FF TREAT_MF TREAT_FM SPILL if grade==`grade', a(strata) cl(school)
	
	foreach group in TREAT_MM TREAT_FF TREAT_MF TREAT_FM {
	scalar t = _b[`group']/_se[`group']
	add_symbol
	
	local diff`group': dis %4.3f _b[`group']
	local diff`group's: dis "`diff`group''`=scalar(symbol)'"
		
	local diffse`group': dis %4.3f _se[`group']
	local diffse`group's: dis "(`diffse`group'')"	
	
		* for grade 12, also report wild-clustered bootstrap p-values
		if `grade' == 12 {
			qui boottest `group', cl(school) qui nogr seed(10101)
			local pbt`group': dis %4.3f r(p)
			local pbt`group's: dis "$\left\lbrace `pbt`group'' \right\rbrace$"
		}
		
		* test difference across groups
		foreach j in MM FF MF {
	    foreach g in FF MF FM {
			if "`j'" ~= "`g'" {
				qui areg `x' TREAT_MM TREAT_FF TREAT_MF TREAT_FM SPILL if grade==`grade', a(strata) cl(school)
				qui test TREAT_`j' = TREAT_`g'
				local pd`j'`g': dis %4.3f r(p)
				local pd`j'`g's: dis "`pd`j'`g''"
			}
		}
	}
	}
	
	* export results 
	if `grade' == 12 {
		mata R = ("`N'", "`meanC'", "`diffTREAT_MMs'" , "`diffTREAT_MFs'", "`diffTREAT_FMs'", "`diffTREAT_FFs'", "`pdMMMFs'", "`pdMMFMs'", "`pdMMFFs'", "`pdMFFMs'", "`pdMFFFs'", "`pdFFFMs'" \ "", "", "`diffseTREAT_MMs'" , "`diffseTREAT_MFs'", "`diffseTREAT_FMs'", "`diffseTREAT_FFs'", "", "", "", "", "", "" \ "", "", "`pbtTREAT_MMs'" , "`pbtTREAT_MFs'", "`pbtTREAT_FMs'", "`pbtTREAT_FFs'", "", "", "", "", "", "")
		mmat2tex R using "$tabfig/balance_grade`grade'_hetgender.tex", append rownames("`Label`x''" "" "")
	}
	if `grade' == 10 {
		mata R = ("`N'", "`meanC'", "`diffTREAT_MMs'" , "`diffTREAT_MFs'", "`diffTREAT_FMs'", "`diffTREAT_FFs'", "`pdMMMFs'", "`pdMMFMs'", "`pdMMFFs'", "`pdMFFMs'", "`pdMFFFs'", "`pdFFFMs'" \ "", "", "`diffseTREAT_MMs'" , "`diffseTREAT_MFs'", "`diffseTREAT_FMs'", "`diffseTREAT_FFs'", "", "", "", "", "", "")
		mmat2tex R using "$tabfig/balance_grade`grade'_hetgender.tex", append rownames("`Label`x''" "")
	}
	}
	}
}


******* HETEROGENEITY ANALYSIS BY GENDER
foreach grade in 10 12 {
	cap erase "$tabfig/main_grade`grade'_hetgender.tex"	
	
	foreach i in primary {
	foreach x of global grade`grade'_`i'_e {		
	* obtain labels
	local Label`x': variable label `x'
	
	* obtain # observations
	qui sum `x' if grade == `grade'
	local N: dis %3.0f r(N)
	local Ns = "`N'"
	
	* obtain mean Control 
	qui sum `x' if CONTROL == 1 & grade==`grade'
	local meanC: dis %3.2f r(mean)
	local meanCs = "`meanC'"
	
	local sdC: dis %3.2f r(sd)
	local sdCs: dis "(`sdC')"
	
	* obtain coef & se of all combinations
	qui areg `x' TREAT_MM TREAT_FF TREAT_MF TREAT_FM SPILL `x'_bl ${grade`grade'_controls} ${grade`grade'_Mdummies} if grade==`grade', a(strata) cl(school)
	
	foreach group in TREAT_MM TREAT_FF TREAT_MF TREAT_FM {
	scalar t = _b[`group']/_se[`group']
	add_symbol
	
	local diff`group': dis %4.3f _b[`group']
	local diff`group's: dis "`diff`group''`=scalar(symbol)'"
		
	local diffse`group': dis %4.3f _se[`group']
	local diffse`group's: dis "(`diffse`group'')"	
	
		* for grade 12, also report wild-clustered bootstrap p-values
		if `grade' == 12 {
			qui boottest `group', cl(school) qui nogr seed(10101)
			local pbt`group': dis %4.3f r(p)
			local pbt`group's: dis "$\left\lbrace `pbt`group'' \right\rbrace$"
		}
		
		* test difference across groups
		foreach j in MM FF MF {
	    foreach g in FF MF FM {
			if "`j'" ~= "`g'" {
				qui areg `x' TREAT_MM TREAT_FF TREAT_MF TREAT_FM SPILL `x'_bl ${grade`grade'_controls} ${grade`grade'_Mdummies} if grade==`grade', a(strata) cl(school)
				qui test TREAT_`j' = TREAT_`g'
				local pd`j'`g': dis %4.3f r(p)
				local pd`j'`g's: dis "`pd`j'`g''"
			}
		}
	}
	}
	
	* export results 
	if `grade' == 12 {
		mata R = ("`N'", "`meanC'", "`diffTREAT_MMs'" , "`diffTREAT_MFs'", "`diffTREAT_FMs'", "`diffTREAT_FFs'", "`pdMMMFs'", "`pdMMFMs'", "`pdMMFFs'", "`pdMFFMs'", "`pdMFFFs'", "`pdFFFMs'" \ "", "", "`diffseTREAT_MMs'" , "`diffseTREAT_MFs'", "`diffseTREAT_FMs'", "`diffseTREAT_FFs'", "", "", "", "", "", "" \ "", "", "`pbtTREAT_MMs'" , "`pbtTREAT_MFs'", "`pbtTREAT_FMs'", "`pbtTREAT_FFs'", "", "", "", "", "", "")
		mmat2tex R using "$tabfig/main_grade`grade'_hetgender.tex", append rownames("`Label`x''" "" "")
	}
	if `grade' == 10 {
		mata R = ("`N'", "`meanC'", "`diffTREAT_MMs'" , "`diffTREAT_MFs'", "`diffTREAT_FMs'", "`diffTREAT_FFs'", "`pdMMMFs'", "`pdMMFMs'", "`pdMMFFs'", "`pdMFFMs'", "`pdMFFFs'", "`pdFFFMs'" \ "", "", "`diffseTREAT_MMs'" , "`diffseTREAT_MFs'", "`diffseTREAT_FMs'", "`diffseTREAT_FFs'", "", "", "", "", "", "")
		mmat2tex R using "$tabfig/main_grade`grade'_hetgender.tex", append rownames("`Label`x''" "")
	}
	}
	}
}


******* HETEROGENEITY ANALYSIS BY GENDER MATCH: SAME VS. CROSS: BASELINE BALANCE

foreach grade in 10 12 {
	cap erase "$tabfig/balance_grade`grade'_match.tex"	
	
	foreach i in primary secondary {
	foreach x of global grade`grade'_`i' {		
	* obtain labels
	local Label`x': variable label `x'
	
	* obtain # observations
	qui sum `x' if grade == `grade'
	local N: dis %3.0f r(N)
	local Ns = "`N'"
	
	* obtain mean Control 
	qui sum `x' if CONTROL == 1 & grade==`grade'
	local meanC: dis %3.2f r(mean)
	local meanCs = "`meanC'"
	
	local sdC: dis %3.2f r(sd)
	local sdCs: dis "(`sdC')"
	
	* obtain coef & se of all combinations
	qui areg `x' TREAT_SAME TREAT_DIFF SPILL if grade==`grade', a(strata) cl(school)
	
	foreach group in TREAT_SAME TREAT_DIFF {
	scalar t = _b[`group']/_se[`group']
	add_symbol
	
	local diff`group': dis %4.3f _b[`group']
	local diff`group's: dis "`diff`group''`=scalar(symbol)'"
		
	local diffse`group': dis %4.3f _se[`group']
	local diffse`group's: dis "(`diffse`group'')"	
	
		* for grade 12, also report wild-clustered bootstrap p-values
		if `grade' == 12 {
			qui boottest `group', cl(school) qui nogr seed(10101)
			local pbt`group': dis %4.3f r(p)
			local pbt`group's: dis "$\left\lbrace `pbt`group'' \right\rbrace$"
		}
		
		* test difference across groups				
		foreach j in SAME {
	    foreach g in DIFF {
			if "`j'" ~= "`g'" {
				qui areg `x' TREAT_SAME TREAT_DIFF if grade==`grade', a(strata) cl(school)
				qui test TREAT_`j' = TREAT_`g'
				local pd`j'`g': dis %4.3f r(p)
				local pd`j'`g's: dis "`pd`j'`g''"
			}
		}
	}
	}
	
	* export results 
	if `grade' == 12 {
		mata R = ("`N'", "`meanC'", "`diffTREAT_SAMEs'" , "`diffTREAT_DIFFs'", "`pdSAMEDIFFs'" \ 
		                   "", "", "`diffseTREAT_SAMEs'" , "`diffseTREAT_DIFFs'",  "" \ 
						   "", "", "`pbtTREAT_SAMEs'" , "`pbtTREAT_DIFFs'", "")
		mmat2tex R using "$tabfig/balance_grade`grade'_match.tex", append rownames("`Label`x''" "" "")
	}
	if `grade' == 10 {
		mata R = ("`N'", "`meanC'", "`diffTREAT_SAMEs'" , "`diffTREAT_DIFFs'", "`pdSAMEDIFFs'" \ "", "", "`diffseTREAT_SAMEs'" , "`diffseTREAT_DIFFs'",  "")
		mmat2tex R using "$tabfig/balance_grade`grade'_match.tex", append rownames("`Label`x''" "")
	}
		}
	}
}


******* HETEROGENEITY ANALYSIS BY GENDER PAIR: SAME VS. CROSS-GENDER
foreach grade in 10 12 {
	cap erase "$tabfig/main_grade`grade'_match.tex"	
	
	foreach i in primary {
	foreach x of global grade`grade'_`i'_e {		
	* obtain labels
	local Label`x': variable label `x'
	
	* obtain # observations
	qui sum `x' if grade == `grade'
	local N: dis %3.0f r(N)
	local Ns = "`N'"
	
	* obtain mean Control 
	qui sum `x' if CONTROL == 1 & grade==`grade'
	local meanC: dis %3.2f r(mean)
	local meanCs = "`meanC'"
	
	local sdC: dis %3.2f r(sd)
	local sdCs: dis "(`sdC')"
	
	* obtain coef & se of all combinations
	qui areg `x' TREAT_SAME TREAT_DIFF SPILL `x'_bl ${grade`grade'_controls} ${grade`grade'_Mdummies} if grade==`grade', a(strata) cl(school)
	
	foreach group in TREAT_SAME TREAT_DIFF {
	scalar t = _b[`group']/_se[`group']
	add_symbol
	
	local diff`group': dis %4.3f _b[`group']
	local diff`group's: dis "`diff`group''`=scalar(symbol)'"
		
	local diffse`group': dis %4.3f _se[`group']
	local diffse`group's: dis "(`diffse`group'')"	
	
		* for grade 12, also report wild-clustered bootstrap p-values
		if `grade' == 12 {
			qui boottest `group', cl(school) qui nogr seed(10101)
			local pbt`group': dis %4.3f r(p)
			local pbt`group's: dis "$\left\lbrace `pbt`group'' \right\rbrace$"
		}
		
		* test difference across groups
		qui test TREAT_SAME = TREAT_DIFF
				local pdSAMEDIFF: dis %4.3f r(p)
				local pdSAMEDIFFs: dis "`pd`comparison''"
				
		foreach j in SAME {
	    foreach g in DIFF {
			if "`j'" ~= "`g'" {
				qui areg `x' TREAT_SAME TREAT_DIFF `x'_bl ${grade`grade'_controls} ${grade`grade'_Mdummies} if grade==`grade', a(strata) cl(school)
				qui test TREAT_`j' = TREAT_`g'
				local pd`j'`g': dis %4.3f r(p)
				local pd`j'`g's: dis "`pd`j'`g''"
			}
		}
	}
	}
	
	* export results 
	if `grade' == 12 {
		mata R = ("`N'", "`meanC'", "`diffTREAT_SAMEs'" , "`diffTREAT_DIFFs'", "`pdSAMEDIFFs'" \ 
		                   "", "", "`diffseTREAT_SAMEs'" , "`diffseTREAT_DIFFs'",  "" \ 
						   "", "", "`pbtTREAT_SAMEs'" , "`pbtTREAT_DIFFs'", "")
		mmat2tex R using "$tabfig/main_grade`grade'_match.tex", append rownames("`Label`x''" "" "")
	}
	if `grade' == 10 {
		mata R = ("`N'", "`meanC'", "`diffTREAT_SAMEs'" , "`diffTREAT_DIFFs'", "`pdSAMEDIFFs'" \ 
		                   "", "", "`diffseTREAT_SAMEs'" , "`diffseTREAT_DIFFs'",  "")
		mmat2tex R using "$tabfig/main_grade`grade'_match.tex", append rownames("`Label`x''" "")
	}
	}
	}
}







******* HETEROGENEITY ANALYSIS BY INITIAL INTEREST AND ACADEMIC PERFORMANCE 

foreach grade in 10 {
	cap erase "$tabfig/main_grade`grade'_hetaca.tex"	
	
	* Comparing based on baseline perceived academic performance in science 
	cap drop d_perf TREATbelowP TREATaboveP 
	su z_perf_scie_bl if grade == `grade', de
	g d_perf = z_perf_scie_bl < r(p75) & ~missing(z_perf_scie_bl) if grade == `grade'
	g TREATbelowP = TREAT*(d_perf==1) if grade == `grade'
	g TREATaboveP = TREAT*(d_perf==0) if grade == `grade'
	
	* Comparing based on baseline perceived academic performance in science 
	cap drop d_like TREATbelowL TREATaboveL
	su z_like_scie_bl if grade == `grade', de
	g d_like = z_like_scie_bl < r(p75) & ~missing(z_like_scie_bl) if grade == `grade'
	g TREATbelowL = TREAT*(d_like==1) if grade == `grade'
	g TREATaboveL = TREAT*(d_like==0) if grade == `grade'
	
	foreach i in primary {
	foreach x of global grade`grade'_`i'_e {		
	* obtain labels
	local Label`x': variable label `x'
	
	* obtain # observations
	qui sum `x' if grade == `grade'
	local N: dis %3.0f r(N)
	local Ns = "`N'"
	
	* obtain mean Control 
	qui sum `x' if CONTROL == 1 & grade==`grade'
	local meanC: dis %3.2f r(mean)
	local meanCs = "`meanC'"
	
	local sdC: dis %3.2f r(sd)
	local sdCs: dis "(`sdC')"
	
	foreach comparison in P L {
	
	qui areg `x' TREATbelow`comparison' TREATabove`comparison' SPILL `x'_bl ${grade`grade'_controls} ${grade`grade'_Mdummies} if grade==`grade', a(strata) cl(school)
	
		foreach group in TREATbelow`comparison' TREATabove`comparison' {
			scalar t = _b[`group']/_se[`group']
			add_symbol
	
			local diff`group': dis %4.3f _b[`group']
			local diff`group's: dis "`diff`group''`=scalar(symbol)'"
		
			local diffse`group': dis %4.3f _se[`group']
			local diffse`group's: dis "(`diffse`group'')"	
		}
		
		* Test of difference 
		qui test TREATbelow`comparison' = TREATabove`comparison'
				local pd`comparison': dis %4.3f r(p)
				local pd`comparison's: dis "`pd`comparison''"
	}
	* export results 
	if `grade' == 10 {
		mata R = ("`N'", "`meanC'", "`diffTREATbelowPs'", "`diffTREATabovePs'", "`pdPs'",  "`diffTREATbelowLs'", "`diffTREATaboveLs'", "`pdLs'" \ ///
		          ""   , "`sdCs'",  "`diffseTREATbelowPs'", "`diffseTREATbelowPs'", "", "`diffseTREATbelowLs'", "`diffseTREATaboveLs'", "")
		mmat2tex R using "$tabfig/main_grade`grade'_hetaca.tex", append rownames("`Label`x''" "")
	}
	}
	}
}


******* HETEROGENEITY ANALYSIS BY INITIAL BIAS AND APPROVAL INDEX 

foreach grade in 12 {
	cap erase "$tabfig/main_grade`grade'_hetbias.tex"	
	
	* Comparing based on baseline perceived academic performance in science 
	cap drop d_bias TREATbelowB TREATaboveB 
	su z_bluecollarbias_bl if grade == `grade', de
	g d_bias = z_bluecollarbias_bl < r(p50) & ~missing(z_bluecollarbias_bl) if grade == `grade' 
	g TREATbelowB = TREAT*(d_bias==1) if grade == `grade'
	g TREATaboveB = TREAT*(d_bias==0) if grade == `grade' 
	
	* Comparing based on baseline perceived academic performance in science 
	cap drop d_approval TREATbelowA TREATaboveA 
	su z_g7b_bl if grade == `grade', de
	g d_approval = z_g7b_bl < r(p50) & ~missing(z_g7b_bl) if grade == `grade' 
	g TREATbelowA = TREAT*(d_approval==1) if grade == `grade'
	g TREATaboveA = TREAT*(d_approval==0) if grade == `grade' 
	
	foreach i in primary {
	foreach x of global grade`grade'_`i'_e {		
	* obtain labels
	local Label`x': variable label `x'
	
	* obtain # observations
	qui sum `x' if grade == `grade'
	local N: dis %3.0f r(N)
	local Ns = "`N'"
	
	* obtain mean Control 
	qui sum `x' if CONTROL == 1 & grade==`grade'
	local meanC: dis %3.2f r(mean)
	local meanCs = "`meanC'"
	
	local sdC: dis %3.2f r(sd)
	local sdCs: dis "(`sdC')"
	
	foreach comparison in B A {
	
	qui areg `x' TREATbelow`comparison' TREATabove`comparison' SPILL `x'_bl ${grade`grade'_controls} ${grade`grade'_Mdummies} if grade==`grade', a(strata) cl(school)
	
		foreach group in TREATbelow`comparison' TREATabove`comparison' {
			scalar t = _b[`group']/_se[`group']
			add_symbol
	
			local diff`group': dis %4.3f _b[`group']
			local diff`group's: dis "`diff`group''`=scalar(symbol)'"
		
			local diffse`group': dis %4.3f _se[`group']
			local diffse`group's: dis "(`diffse`group'')"	
			
			* for grade 12, also report wild-clustered bootstrap p-values
			if `grade' == 12 {
				qui boottest `group', cl(school) qui nogr seed(10101)
				local pbt`group': dis %4.3f r(p)
				local pbt`group's: dis "$\left\lbrace `pbt`group'' \right\rbrace$"
			}
		}
		
		* Test of difference 
		qui test TREATbelow`comparison' = TREATabove`comparison'
				local pd`comparison': dis %4.3f r(p)
				local pd`comparison's: dis "`pd`comparison''"
	}
	* export results 
	if `grade' == 12 {
		mata R = ("`N'", "`meanC'", "`diffTREATbelowBs'", "`diffTREATaboveBs'", "`pdBs'",  "`diffTREATbelowAs'", "`diffTREATaboveAs'", "`pdAs'" \ ///
		          ""   , "`sdCs'",  "`diffseTREATbelowBs'", "`diffseTREATbelowBs'", "", "`diffseTREATbelowAs'", "`diffseTREATaboveAs'", "" \ /// 
				  ""   , "" ,  "`pbtTREATbelowBs'", "`pbtTREATaboveBs'", "", "`pbtTREATbelowAs'", "`pbtTREATaboveAs'", "")
		mmat2tex R using "$tabfig/main_grade`grade'_hetbias.tex", append rownames("`Label`x''" "" "")
	}
	}
	}
}



********************************************************************************
********************************************************************************
**************************   LEE BOUNDS	          ******************************
********************************************************************************
********************************************************************************

cap program drop add_symbol3 
program add_symbol3
	if `=scalar(t)' < 1.645 {
		scalar symbol = ""
	}
	if `=scalar(t)' > 1.645 & `=scalar(t)' <= 1.96 {
		scalar symbol = "*"
	}
	if `=scalar(t)' > 1.96 & `=scalar(t)' <= 2.576 {
		scalar symbol = "**"
	}
	if `=scalar(t)' > 2.576 {
		scalar symbol = "***"
	}
end

gl grade10_bound_e "i_sci_top1"
gl grade12_bound_e "i_f9g i_f14g" 


foreach grade in 10 12 {
	cap erase "$tabfig/main_grade`grade'_leebounds.tex"
	foreach i in bound  {
	foreach x of global grade`grade'_`i'_e {	
		
	* obtain labels
	local Label`x': variable label `x'
	
	* obtain # observations
	qui sum `x' if grade == `grade'
	local N: dis %3.0f r(N)
	local Ns = "`N'"
	
	cap drop attrited 
	cap drop selection 
	gen attrited = (`x' == .) & (`x'_bl ~= .) 
		
	* Lee bounds 
	gen selection = attrited == 0	
	
	leebounds `x' TREAT if grade == `grade', select(selection) cieffect tight(`x'_bl)

	foreach group in lower upper {
		scalar t = _b[`group']/_se[`group']
		add_symbol3
		
		local diff`group': dis %4.3f _b[`group']
		local diff`group's: dis "`diff`group''`=scalar(symbol)'"
		
		local diffse`group': dis %4.3f _se[`group']
		local diffse`group's: dis "(`diffse`group'')"	
	
	}
	* export results 	
	mata R = ("`N'", "`difflowers'" , "`diffuppers'"  \ "", "`diffselowers'" , "`diffseuppers'" )
	mmat2tex R using "$tabfig/main_grade`grade'_leebounds.tex", append rownames("`Label`x''" "")
	}
	}
}

cap log close


		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
